<template>
	<view class="login_box">
		<view class="input_box">
			<input type="text" placeholder="请输入姓名" placeholder-style="color:#bebfc3" disabled="true" v-model="name">
			<input type="number" placeholder="请输入手机号" maxlength="11" placeholder-style="color:#bebfc3" v-model="phone">
			<input type="idcard" placeholder="请输入身份证号" placeholder-style="color:#bebfc3" disabled="true" v-model="idcard">
			<input type="number" placeholder="请输入银行卡号" placeholder-style="color:#bebfc3" v-model="card_code">
		</view>
		<view class="login_button" @click="_card">
			绑定
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '', //姓名
				phone: '', //手机号
				idcard: '', //身份证号
				card_code: '', //银行卡号
				card_code_id: null, //银行卡号ID
				userInfo: {},
			}
		},
		onLoad() {
			this.userInfo = uni.getStorageSync('userinfo');
			this.name = this.userInfo.nickname
			this.idcard = this.userInfo.idcards
		},
		methods: {
			_card(){
				if (this.name == '') {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				if (this.phone == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none',
						duration: 2000
					})
					return false
				} else {
					if(!(/^1[3-9]\d{9}$/.test(this.phone))){
						uni.showToast({
							title: '请输入正确的手机号',
							icon: 'none',
							duration: 2000
						})
						return false
					}
				}
				if (this.idcard == '') {
					uni.showToast({
						title: '请输入身份证号',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				if (this.card_code == '') {
					uni.showToast({
						title: '请输入银行卡号',
						icon: 'none',
						duration: 2000
					})
					return false
				}
				this.card()
			},
			async card() {
				// 发送请求
				uni.showLoading({
					title: '加载中'
				});
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp5({
					url: 'Bankcard/bankCardSave',
					data: {
						token:userinfo.token,
						name: this.name,
						phone: this.phone,
						idcards: this.idcard,
						bank_card_number: this.card_code,
						bank_card_id: this.card_code_id
					},
					header: {
						'token': userinfo.token
					},
				});
				uni.hideLoading()
				if (res.code == 1) {
					userinfo.is_bank = 1
					uni.setStorageSync('userinfo', userinfo);
					uni.showToast({
						title: '绑定成功',
						icon: 'none',
						duration: 2000,
						success() {
							setTimeout(function() {
								uni.navigateBack({
									delta: 1 // 返回的页面数，如果为1表示返回上一页
								});
							}, 1500)
						}
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login_box {
		min-height: 100vh;
		padding: 80rpx 40rpx;
		box-sizing: border-box;
		background-image: linear-gradient(#f4f4f5, #f4f4f5);

		.input_box {
			background-color: #fff;
			border-radius: 15rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			input {
				width: 90%;
				padding: 15rpx;
				height: 100rpx;
				box-sizing: border-box;
			}

			input:not(:last-child) {
				border-bottom: #dddddf solid 1px;
			}

			.yzm_box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 90%;
				border-bottom: #dddddf solid 1px;

				input {
					width: 70%;
					border-bottom: none;
				}

				text {
					color: #abeaeb;
					font-size: 24rpx;
					display: block;
					margin-right: 15rpx;
				}
			}
		}

		.login_button {
			background-color: #2979ff;
			width: 100%;
			border-radius: 15rpx;
			font-size: 28rpx;
			padding: 30rpx;
			box-sizing: border-box;
			color: #fff;
			text-align: center;
			margin-top: 45rpx;
			transition: all 0.5s;
		}

		.login_button:active {
			background-color: #9cd3d4;
		}

		.label {
			margin-top: 30rpx;
			display: flex;
			align-items: center;
			font-size: 24rpx;
			color: #818181;

			text {
				color: #9cd3d4;
				font-size: 30rpx;
			}
		}
	}
</style>
